// pages/confirm/confirm.js
var app = getApp()
var host = app.globalData.host
var self = null
Page({
  data: {
    info: null,
    itemId: '',
    productId: '',
    groupId: '',
    count: 1,
    userName: null,
    postalCode: null,
    provinceName: null,
    cityName: null,
    countyName: null,
    detailInfo: null,
    nationalCode: null,
    telNumber: null,
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    numbers: [],
    total: 0
  },
  onLoad: function (options) {
    self = this;
    self.setData({
      itemId: options.itemId ? options.itemId : '',
      productId: options.productId ? options.productId : '',
      groupId: options.groupId ? options.groupId : ''
    })
    self.getStyle();
    self.getNumbers();
    self.getInfo();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  getNumbers() {
    var temp = []
    for (var i = 1; i <= 10; i++) {
      temp.push(i)
    }
    self.setData({
      numbers: temp
    })
  },
  address() {
    wx.chooseAddress({
      success: function (res) {
        self.setData({
          userName: res.userName,
          postalCode: res.postalCode,
          provinceName: res.provinceName,
          cityName: res.cityName,
          countyName: res.countyName,
          detailInfo: res.detailInfo,
          nationalCode: res.nationalCode,
          telNumber: res.telNumber,
        })
      }
    })
  },
  decrease() {
    var count = this.data.count
    if (count <= 1) {
      count = 1;
    } else {
      count -= 1;
    }
    this.setData({
      count: count
    })
    self.counter()
  },
  plus() {
    var count = this.data.count
    count += 1;
    this.setData({
      count: count
    })
    self.counter()
  },
  pickerChange(e) {
    var count = parseInt(e.detail.value)
    self.setData({
      count: count
    })
    self.counter()
  },
  counter() {
    var price = self.data.info.product ? self.data.info.product.price.toFixed(2) : 0;
    var count = parseInt(self.data.count)
    var total = price * count
    self.setData({
      total: total
    })
  },
  getInfo() {
    var url = null;
    var params = null;
    if (self.data.itemId) {
      url = host + '/yq/item/info'
      params = {
        itemId: self.data.itemId
      }
    } else if (self.data.productId) {
      url = host + '/yq/product/info'
      params = {
        productId: self.data.productId
      }
    }
    var success = function (res) {
      console.log(res);
      self.setData({
        info: res.info
      })
      self.counter();
    };
    var fail = function (res) { };
    var complete = function () { };
    app.request(url, params, success, fail, complete);
  },
  onSubmit() {
    if (!wx.getStorageSync("openId")) {
      wx.showModal({
        title: '提示',
        content: '还没允许获取用户信息',
        confirmText: '去设置',
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
              }
            })
          }
        }
      })
      return;
    }
    if (!self.data.userName || !self.data.telNumber || !self.data.provinceName) {
      wx.showToast({
        title: '请填写收货地址',
        icon: 'none'
      })
      return;
    }
    wx.showLoading({
      title: '支付中',
    })
    var url = host + '/yq/order/pay';
    var params = {
      userName: self.data.userName,
      postalCode: self.data.postalCode,
      provinceName: self.data.provinceName,
      cityName: self.data.cityName,
      countyName: self.data.countyName,
      detailInfo: self.data.detailInfo,
      nationalCode: self.data.nationalCode,
      telNumber: self.data.telNumber,
      count: this.data.count
    }
    if (self.data.productId) {
      params.productId = self.data.productId;
    }
    if (self.data.itemId) {
      params.itemId = self.data.itemId;
    }
    if (self.data.groupId) {
      params.groupId = self.data.groupId;
    }
    var success = function (res) {
      wx.hideLoading()
      var response = res.info
      console.log(response)
      var nonce_str = response.nonce_str
      var prepay_id = response.package
      var sign = response.sign
      var timestamp = response.timeStamp
      var appId = response.appid
      var param = {
        'appId': appId,
        'timeStamp': '' + timestamp,
        'nonceStr': '' + nonce_str,
        'package': 'prepay_id=' + prepay_id,
        'signType': 'MD5',
        'paySign': '' + sign,
        'success': function (res) {
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/success/success?isFromMore=1'
            })
          }, 200)
        },
        'fail': function (res) {
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          })
        }
      }
      wx.requestPayment(param)
    }
    var fail = function (res) {};
    var complete = function () {};
    app.request(url, params, success, fail, complete);
  }
})